package linter.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import linter.POJO.DO.ModelUser;
import linter.mapper.ModeUserlMapper;
import linter.service.ModelUserService;
import linter.utils.PropertyUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.MultiValueMap;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

import static linter.custom.Constant.LINEAR;
import static linter.custom.Constant.LOGISTIC_REGRESSION;

/**
* @author 北煜
* @description 针对表【model】的数据库操作Service实现
* @createDate 2024-03-12 10:57:10
*/
@Service
public class ModelUserServiceImpl extends ServiceImpl<ModeUserlMapper, ModelUser>
    implements ModelUserService {

    @Resource
    ModeUserlMapper modeUserlMapper;

    @Override
    public String saveDataLinear(String result, MultiValueMap params, Long uuid) {
        Date date = new Date();
        ModelUser modelUser = new ModelUser();
        // 进行序列化操作
        ObjectMapper objectMapper = new ObjectMapper();
        Map<String, List<Object>> map = params.toSingleValueMap();
        try {
            String json = objectMapper.writeValueAsString(map);
            modelUser.setSelModelParameters(json);
            modelUser.setUuid(uuid);
            modelUser.setPredictresult(result);
            int modelNub = LINEAR;
            modelUser.setModelNub(modelNub);
            modelUser.setCreateTime(date);
            modelUser.setUpdateTime(date);
            int insertNub = modeUserlMapper.insertByUuid(modelUser);
            return insertNub != 1 ? "保存线性回归数据失败" : "保存线性回归数据成功";
        } catch (JsonProcessingException e) {
            throw new RuntimeException("MultiValueMap转换为JSON失败" + e);
        }
    }

    @Override
    public String saveDataLM(String result, MultiValueMap params, Long uuid) {
        Date date = new Date();
        ModelUser modelUser = new ModelUser();
        // 进行序列化操作
        ObjectMapper objectMapper = new ObjectMapper();
        Map<String, List<Object>> map = params.toSingleValueMap();
        try {
            String json = objectMapper.writeValueAsString(map);
            modelUser.setSelModelParameters(json);
            modelUser.setUuid(uuid);
            modelUser.setPredictresult(result);
            int modelNub = LOGISTIC_REGRESSION;
            modelUser.setModelNub(modelNub);
            modelUser.setCreateTime(date);
            modelUser.setUpdateTime(date);
            int insertNub = modeUserlMapper.insert(PropertyUtils.doToDto(modelUser, ModelUser.class));
            return insertNub != 1 ? "保存逻辑回归数据失败" : "保存逻辑回归数据成功";
        } catch (JsonProcessingException e) {
            throw new RuntimeException("MultiValueMap转换为JSON失败" + e);
        }
    }
}




